Unit 7: Feature Selection - Filter Methods

Introduction

As datasets grow in complexity, we often face the Curse of Dimensionality — a phenomenon where high-dimensional data becomes sparse, making distance-based algorithms like KNN perform poorly. The performance of machine learning models degrades as the number of features increases without a proportional increase in data.

Key Question: How can we reduce the number of features while preserving the predictive power of our model?

Two main approaches combat this curse:

In this lecture, we focus on Filter Methods — fast, model-agnostic statistical techniques for selecting the most informative features before training any machine learning model.

Theory

The Feature Selection Problem

Given a feature matrix \(X \in \mathbb{R}^{n \times d}\) with \(n\) samples and \(d\) features, and target variable \(y \in \mathbb{R}^n\), our goal is to select a subset \(S \subseteq \{1, 2, ..., d\}\) such that features in \(S\) are most predictive of \(y\).

Exponential Search Space: There are \(2^d\) possible subsets of \(d\) features. For \(d = 30\), we must consider \(2^{30} = 1,073,741,824\) subsets — exhaustive search is computationally infeasible!

Filter vs Wrapper Methods

Characteristic Filter Methods Wrapper Methods
Timing Preprocessing step, independent of ML algorithm "Wraps" around a specific ML algorithm
Evaluation Statistical measures (correlation, chi-square) Actual model performance (cross-validation)
Speed Fast and scalable Slower but more accurate
Model Dependence Algorithm-agnostic Tailored to specific algorithm

Note: A third category called "Embedded Methods" (e.g., LASSO, Random Forest feature importance) exists where feature selection happens during model training.

How Filter Methods Work

  1. Calculate a statistical score for each feature independently
  2. Rank features by their scores (higher = better)
  3. Select top-k features or apply a threshold
  4. Train the model on the selected features

1. Variance Threshold

The simplest filter method removes features with very low variance:

\[ \text{Var}(X_j) = \frac{1}{n} \sum_{i=1}^{n} (x_{ij} - \bar{x}_j)^2 \]

Important: Standardization or normalization is required before applying variance threshold so that the same threshold works for all features. A threshold between 0.01 and 0.1 is generally effective.

2. Chi-Square (\(\chi^2\)) Filter

The Chi-Square test measures the association between a categorical feature and a categorical target variable.

\[ \chi^2 = \sum_{i=1}^{c} \frac{(O_i - E_i)^2}{E_i} \]

Where \(c\) is the degree of freedom, \(O_i\) is the observed frequency, and \(E_i\) is the expected frequency in cell \(i\).

The expected frequency is computed as:

\[ E_{ij} = \frac{(\text{Row Total}_i) \times (\text{Column Total}_j)}{\text{Grand Total}} \]

3. ANOVA F-Test

ANOVA (Analysis of Variance) is used when:

\[ F = \frac{\text{Between-group variability}}{\text{Within-group variability}} = \frac{SS_{between} / (K-1)}{SS_{within} / (N-K)} \]

Where \(K\) = number of classes, \(N\) = total samples.

Higher F-statistic indicates greater difference between class means, making the feature more informative.

4. Mutual Information (MI)

Mutual Information measures how much knowing one variable reduces uncertainty about another. It works with any feature type (categorical or continuous).

\[ MI(X; Y) = \sum_{x \in X} \sum_{y \in Y} p(x, y) \log \frac{p(x, y)}{p(x) p(y)} \]

Comparison of Filter Methods

Method Target Type Feature Type Captures Non-linear Model-agnostic
Variance Threshold Any Any No Yes
Correlation Continuous Continuous No Yes
Chi-Square Categorical Categorical No Yes
Mutual Information Any Any Yes Yes
ANOVA F-test Categorical Continuous No Yes

Interactive Examples

Example 1: Understanding the Search Space

Problem: For \(d = 3, 4,\) and \(5\) features, how many possible subsets exist? List all subsets for \(d = 3\).

Solution:

For \(d = 3\) with features \(\{A, B, C\}\):

Example 2: Chi-Square Test in Action

Dataset: Examining the relationship between "Income Level" (Low, Medium, High) and "Subscription Status" (Subscribed, Not Subscribed).

Income Level Subscribed (O) Not Subscribed (O) Row Total
Low 20 30 50
Medium 40 25 65
High 10 15 25
Column Total 70 70 140

Expected Values:

Numerical Solutions

Problem 1: Complete Chi-Square Calculation

Using the contingency table from Example 2, compute the Chi-Square statistic and determine if Income Level is a significant predictor of Subscription Status at \(\alpha = 0.05\).

Step 1: State Hypotheses
  • H\(_0\): No significant association between Income Level and Subscription Status
  • H\(_1\): There is a significant association between Income Level and Subscription Status
Step 2: Calculate Expected Frequencies
Income Level Subscribed (E) Not Subscribed (E)
Low2525
Medium32.532.5
High12.512.5

Formula: \(E_{ij} = \frac{\text{Row}_i \times \text{Column}_j}{\text{Grand Total}}\)

Step 3: Compute Chi-Square Statistic
\[ \chi^2 = \sum \frac{(O - E)^2}{E} \]
  • Low, Subscribed: \((20 - 25)^2 / 25 = 1.0\)
  • Low, Not Subscribed: \((30 - 25)^2 / 25 = 1.0\)
  • Medium, Subscribed: \((40 - 32.5)^2 / 32.5 = 1.731\)
  • Medium, Not Subscribed: \((25 - 32.5)^2 / 32.5 = 1.731\)
  • High, Subscribed: \((10 - 12.5)^2 / 12.5 = 0.5\)
  • High, Not Subscribed: \((15 - 12.5)^2 / 12.5 = 0.5\)

Total: \(\chi^2 = 6.462\)

Step 4: Determine Degrees of Freedom and Critical Value
\[ df = (r - 1) \times (c - 1) = (3 - 1) \times (2 - 1) = 2 \]

Critical value at \(\alpha = 0.05\), \(df = 2\): 5.991

Step 5: Conclusion

Since \(6.462 > 5.991\), we reject H\(_0\).

Conclusion: There is a significant association between Income Level and Subscription Status. This feature would be selected by the Chi-Square filter method. Higher \(\chi^2\) values indicate a stronger feature-target relationship.

Try-It-Yourself Problems

Problem 1: Variance Threshold Decision

You have 4 features with the following variances after standardization: A=0.15, B=0.003, C=0.08, D=0.001. If you apply VarianceThreshold with threshold=0.01, which features will be selected?

Features with variance >= 0.01 are selected:

  • A: 0.15 >= 0.01 SELECTED
  • B: 0.003 < 0.01 REJECTED
  • C: 0.08 >= 0.01 SELECTED
  • D: 0.001 < 0.01 REJECTED

Selected features: A and C

Problem 2: ANOVA F-test Interpretation

You compute ANOVA F-statistics for three features predicting a binary target:

Using \(\alpha = 0.05\), which features are significant? Rank them by importance.

Compare each p-value to \(\alpha = 0.05\):

  • Feature X: p = 0.0004 < 0.05 SIGNIFICANT
  • Feature Y: p = 0.15 > 0.05 NOT SIGNIFICANT
  • Feature Z: p = 0.004 < 0.05 SIGNIFICANT

Ranking by F-statistic (higher = more important):

  1. Feature X (F = 12.5)
  2. Feature Z (F = 8.3)
  3. Feature Y (F = 2.1) — not significant

Problem 3: Method Selection

For each scenario, identify the most appropriate filter method:

  1. Predicting loan default (Yes/No) using customer age (continuous) and income (continuous).
  2. Predicting disease presence (Yes/No) using blood type (A, B, AB, O) and genotype categories.
  3. Identifying which sensors provide useful information when some sensors always read the same value.
  1. ANOVA F-test — continuous features, categorical target.
  2. Chi-Square — both feature and target are categorical.
  3. Variance Threshold — removes constant/quasi-constant features.

Problem 4: Mutual Information Comparison

Suppose you calculate Mutual Information scores for four features:

If you need to select the top 2 features, which do you choose? What does MI = 0.0 tell you about Feature A?

Top 2 features by MI score: Feature D (0.78) and Feature B (0.45)

Feature A with MI = 0.0: This indicates that Feature A is completely independent of the target variable. Knowing Feature A provides zero information about the target. It should be dropped.

Interactive Quiz

Test your understanding of Filter Methods. Select the best answer for each question.

Q1. What is the primary advantage of filter methods over wrapper methods?

They always produce better model accuracy
They are faster and model-independent
They can capture feature interactions
They require less training data

Q2. How many possible feature subsets exist for a dataset with 20 features?

20
400
1,048,576
20!

Q3. Which filter method is appropriate for a categorical feature and categorical target?

ANOVA F-test
Chi-Square test
Pearson Correlation
Variance Threshold

Q4. What does a higher F-statistic in ANOVA indicate about a feature?

The feature has low variance
Greater difference between class means
The feature is normally distributed
The feature has many missing values

Q5. Which filter method can handle both categorical and continuous features?

Chi-Square
ANOVA F-test
Mutual Information
Correlation Coefficient

Key Takeaways

Common Pitfalls

Resources